package cn.edu.gdpt.mobile213.mobile2121606097wjc0515;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class SingleIeInstanceActivity extends AppCompatActivity implements View.OnClickListener{
    private TextView tvTitle;
    private TextView tvTaskID;
    private TextView tvActivityNum;
    private Button btnStandard;
    private Button btnSingleTop;
    private Button btnSingleTask;
    private Button btnSingleInstance;
    private Button btnClose;
    private RecyclerView rvActivities;
    private static int  NO=0;
    public static List<String>list=new ArrayList<>();
    public static ActivityAdapter adapter=new ActivityAdapter(list);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_single_ie_instance);
        initView();

        NO+=1;
        tvTitle.setText("SingleInstanceActivity"+NO);

        list.add(tvTitle.getText().toString());
        if (rvActivities.getAdapter()==null){
            rvActivities.setAdapter(adapter);
        }else {
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    protected void onStart(){
        super.onStart();
        tvTaskID.setText("任务栈ID:"+getTaskId());
        ActivityManager activityManager=(ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        ActivityManager.AppTask appTask=activityManager.getAppTasks().get(0);
        ActivityManager.RecentTaskInfo taskInfo=appTask.getTaskInfo();
        int num = taskInfo.numActivities;
        tvActivityNum.setText("Activity的数量:"+num);
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        list.remove(tvTaskID.getText().toString());
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onRestart(){
        super.onRestart();
        adapter.notifyDataSetChanged();
    }

    private void initView() {
        tvTitle=(TextView) findViewById(R.id.tvTitle);
        tvTaskID=(TextView) findViewById(R.id.tvTaskID);
        tvActivityNum=(TextView) findViewById(R.id.tvActivityNum);
        btnStandard=(Button) findViewById(R.id.btnStandard);
        btnSingleTop=(Button) findViewById(R.id.btnSingleTop);
        btnSingleTask=(Button) findViewById(R.id.btnSingleTask);
        btnSingleInstance=(Button) findViewById(R.id.btnSingleInstance);
        btnClose=(Button) findViewById(R.id.btnClose);
        rvActivities=(RecyclerView) findViewById(R.id.rvActivities);
        btnStandard.setOnClickListener(this);
        btnSingleTop.setOnClickListener(this);
        btnSingleTask.setOnClickListener(this);
        btnSingleInstance.setOnClickListener(this);
        LinearLayoutManager layout=new LinearLayoutManager(this);
        layout.setStackFromEnd(true);
        layout.setReverseLayout(true);
        rvActivities.setLayoutManager(layout);
    }

    @Override
    public void onClick(View view){
        Intent intent=new Intent();
        switch (view.getId()){
            case R.id.btnStandard:
                intent.setClass(this,SingleTopActivity.class);
                startActivity(intent);
                break;
            case R.id.btnSingleTop:
                intent.setClass(this,SingleTopActivity.class);
                startActivity(intent);
                break;
            case R.id.btnSingleTask:
                intent.setClass(this,SingleTsakActivity.class);
                startActivity(intent);
                break;
            case R.id.btnSingleInstance:
                intent.setClass(this,SingleIeInstanceActivity.class);
                startActivity(intent);
                break;
        }
    }
}